﻿Imports DAO
Imports BUS
Imports DTO
Imports DevComponents.DotNetBar
Imports Microsoft.VisualBasic
Imports System
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Data
Imports System.Drawing
Imports System.Text
Imports System.Windows.Forms
Imports System.IO
Imports System.Diagnostics

Public Class frmQuanLySach

    Private Sub frmQuanLySach_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ColCmbTheLoai.ValueMember = "MaTheLoai"
        ColCmbTheLoai.DisplayMember = "TenTheLoai"
        ColCmbTheLoai.DataSource = TheLoaiDAO.GetTheLoaiAll()
        cmbTheLoai.ValueMember = "MaTheLoai"
        cmbTheLoai.DisplayMember = "TenTheLoai"
        cmbTheLoai.DataSource = TheLoaiDAO.GetTheLoaiAll()
        cmbTimTheLoai.ValueMember = "MaTheLoai"
        cmbTimTheLoai.DisplayMember = "TenTheLoai"
        cmbTimTheLoai.DataSource = TheLoaiDAO.GetTheLoaiAll()
        HienThiDanhSach()
    End Sub

    Public Sub HienThiDanhSach()
        dgvThongTinDauSach.DataSource = SachDAO.SelectSachAll()
    End Sub


    Private Sub btnThem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnThem.Click
        Dim s As New SachDTO()
        s.TenSach = txtTenSach.Text
        s.TacGia = txtTacGia.Text
        Try
            s.SoLuongTon = Integer.Parse(txtSoLuongTon.Text)

        Catch e1 As FormatException
            MessageBoxEx.Show("Nhập vào số lượng tồn")
            Return
        End Try
        s.GiaBan = Integer.Parse(txtGiaBan.Text)
        s.MaTheLoai = Integer.Parse(cmbTheLoai.SelectedValue.ToString())

        If SachBUS.ThemSach(s) = False Then
            MessageBoxEx.Show("Sách dã tồn tại trong CSDL")
        End If
        HienThiDanhSach()
    End Sub

    Private Sub dgvThongTinDauSach_SelectionChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles dgvThongTinDauSach.SelectionChanged
        If dgvThongTinDauSach.SelectedRows.Count > 0 Then
            txtMaSach.Text = dgvThongTinDauSach.SelectedRows(0).Cells(0).Value.ToString()
            txtTenSach.Text = dgvThongTinDauSach.SelectedRows(0).Cells(1).Value.ToString()
            txtGiaBan.Text = dgvThongTinDauSach.SelectedRows(0).Cells(2).Value.ToString()
            txtSoLuongTon.Text = dgvThongTinDauSach.SelectedRows(0).Cells(3).Value.ToString()
            txtTacGia.Text = dgvThongTinDauSach.SelectedRows(0).Cells(4).Value.ToString()
            cmbTheLoai.SelectedValue = dgvThongTinDauSach.SelectedRows(0).Cells(5).Value
        End If
    End Sub

    Private Sub btnThoat_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnThoat.Click
        Me.Close()
    End Sub

    Private Sub btnTimTatCa_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTimTatCa.Click
        HienThiDanhSach()
    End Sub

    Private Sub btnXoaTrang_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnXoaTrang.Click
        txtMaSach.Clear()
        txtTenSach.Clear()
        txtGiaBan.Clear()
        txtSoLuongTon.Clear()
        txtTacGia.Clear()
    End Sub

    Private Sub cmbTimTheLoai_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmbTimTheLoai.SelectedIndexChanged
        Dim i As String = cmbTimTheLoai.SelectedValue.ToString()
        HienThiDanhSach2(i)
    End Sub

    Public Sub HienThiDanhSach2(ByVal ma As String)
        Dim s As New SachDTO()
        s.MaTheLoai = Integer.Parse(ma)
        dgvThongTinDauSach.DataSource = SachDAO.SelectSachLikeMaTheLoai(s)
    End Sub

    Private Sub btnXoa_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnXoa.Click
        Dim s As New SachDTO()
        Try
            s.MaSach = Integer.Parse(txtMaSach.Text)
        Catch
            MessageBoxEx.Show("Mã sách không được để trống, chọn từ danh sách bên dưới để cập nhật")
        End Try
        If SachBUS.XoaSach(s) = False Then
            MessageBoxEx.Show("Mã sách không tồn tại trong CSDL")
        End If
        HienThiDanhSach()
        MessageBoxEx.Show("Xóa thành công")
    End Sub

    Private Sub btnSua_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSua.Click
        Dim s As New SachDTO()
        Try
            s.MaSach = Integer.Parse(txtMaSach.Text)
        Catch
            MessageBoxEx.Show("Mã sách không được để trống, chọn từ danh sách bên dưới để cập nhật")
        End Try

        s.TenSach = txtTenSach.Text
        s.TacGia = txtTacGia.Text
        Try
            s.SoLuongTon = Integer.Parse(txtSoLuongTon.Text)

        Catch e1 As FormatException
            MessageBoxEx.Show("Số lượng tồn không được để trống và điền vào số")
            Return
        End Try
        s.GiaBan = Integer.Parse(txtGiaBan.Text)
        s.MaTheLoai = Integer.Parse(cmbTheLoai.SelectedValue.ToString())

        If SachBUS.CapNhatSach(s) = False Then
            MessageBoxEx.Show("Mã sách không tồn tại trong CSDL")
        End If
        HienThiDanhSach()
        MessageBoxEx.Show("Cập nhật thành công")
    End Sub

    Private Sub btnXuat_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnXuat.Click
        Dim oldPath As String = Directory.GetCurrentDirectory()
        Dim sf As New SaveFileDialog()
        sf.ShowDialog()
        Dim strFileName As String = sf.FileName
        Directory.SetCurrentDirectory(oldPath)
        Dim myExcel As New ExcelWrite()
        Dim list As List(Of String) = myExcel.DGVToExcel(dgvThongTinDauSach)
        Dim filePath As String = Directory.GetCurrentDirectory() & strFileName
        myExcel.WriteDateToExcel(strFileName, list, "A1", "F1", "A2")
        MessageBoxEx.Show("Xuất danh sách khách hàng ra file Excel thành công", "Thông báo")
    End Sub

    Private Sub btnNhap_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNhap.Click
        Dim oldPath As String = Directory.GetCurrentDirectory()
        Dim [of] As New OpenFileDialog()
        [of].ShowDialog()
        Dim strPathName As String = [of].FileName
        Directory.SetCurrentDirectory(oldPath)
        Dim dt As DataTable = ExcelRead.getSheet(strPathName, "sheet1")
        For i As Integer = 0 To dt.Rows.Count - 1
            Dim s As New SachDTO()
            s.TenSach = dt.Rows(i).ItemArray(1).ToString()
            s.GiaBan = Integer.Parse(dt.Rows(i).ItemArray(2).ToString())
            s.SoLuongTon = Integer.Parse(dt.Rows(i).ItemArray(3).ToString())
            s.TacGia = dt.Rows(i).ItemArray(4).ToString()
            s.MaTheLoai = Integer.Parse(dt.Rows(i).ItemArray(5).ToString())
            SachDAO.Insert(s)
        Next i
        MessageBoxEx.Show("Nhập danh sách khách hàng từ Excel thành công", "Thông báo")
        HienThiDanhSach()
    End Sub
End Class